#-*-coding:utf-8-*-
#值传递
# l1 = [1,2,3]
# l2 = [1,2,3]
# l3 = l2
# print l1 is l2
# print l2 is l3
# print "id {} {} {}".format(str(id(l1)),str(id(l2)),str(id(l3)))
import random
import math
# 按照权重产生随机数
def randomChoiceByWeight(weightList, chooseCount=1):
	idxList = []
	preSum = []
	sum = 0
	preSum.append(sum)
	for insectID, weight in weightList:
		idxList.append(insectID)
		sum += weight
		preSum.append(sum)

	result = []
	for _ in xrange(chooseCount):
		index = pickIndex(preSum)
		print "index = {}".format(index)
		result.append(idxList[index - 1])

	return result


def pickIndex( preSum):
	randNum = random.randint(1, preSum[-1])
	left = 0
	right = len(preSum) - 1
	while left < right:
		mid = left + (right - left) // 2
		print "mid = {},randNum = {}".format(mid,randNum)
		if preSum[mid] <= randNum:
			left = mid + 1
		else:
			right = mid
	return left

if __name__ == "__main__":
	pass
	# weightList = ((1, 10), (2, 10), (3, 10), (4, 10), (5, 10), (6, 10), (7, 10), (8, 10), (9, 5), (10, 2), (11, 1))
	# for i in xrange(1):
	# 	ret = randomChoiceByWeight(weightList,8)
	# 	print "ret = {},idex = {}".format(ret,i)
	# pass
	# print [3,4,7,6,9,2,5,10]*2
	tmpList = [1,2,3,4,5]
	# num = tmpList[-1]
	# tmpList.pop(-1)
	num = tmpList.pop()
	print num ,tmpList